"""
导出剧本数据库
用于更新以下页面：
https://wiki.biligame.com/umamusume/%E6%A8%A1%E5%9D%97:%E6%97%A5%E6%9C%8D%E5%89%A7%E6%9C%AC%E6%95%B0%E6%8D%AE%E5%BA%93%E5%86%85%E5%AE%B9
https://wiki.biligame.com/umamusume/%E6%A8%A1%E5%9D%97:%E7%B9%81%E4%B8%AD%E5%89%A7%E6%9C%AC%E6%95%B0%E6%8D%AE%E5%BA%93%E5%86%85%E5%AE%B9
https://wiki.biligame.com/umamusume/%E6%A8%A1%E5%9D%97:%E7%AE%80%E4%B8%AD%E5%89%A7%E6%9C%AC%E6%95%B0%E6%8D%AE%E5%BA%93%E5%86%85%E5%AE%B9
"""
import sqlite3

file = open("export_data/scenario.txt","w",encoding='utf-8')
file.write("local p={}\n")
category = 6
con = sqlite3.connect("database/master_CN.mdb")  # 其他服务器请更换
cur = con.cursor()
def sql2lua(table_name,category=None):
    # 首先统计表名
    name_array = []
    cur.execute("PRAGMA table_info([" + table_name + "])")
    for x in cur:
        name_array.append(x[1])
    print(name_array)
    # 读取表内容
    if category==None:
        file.write("p."+table_name+"={\n")
        cur.execute("select * from " + table_name)
    else:
        file.write("p." + table_name + "_" + str(category) +"={\n")
        cur.execute("select * from "+table_name+" where category = %d" %category)

    is_first = True
    for x in cur:
        if is_first:
            is_first = False
        else:
            file.write(",\n")
        text_is_first = True
        file.write("{")
        for i in range(len(name_array)):
            if text_is_first:
                text_is_first = False
            else:
                file.write(",")
            file.write(name_array[i] + "=\"" + str(x[i]) + "\"")
        file.write("}")
    file.write("\n}\n")

# 导出single_mode_training_effect
sql2lua("single_mode_training_effect")
sql2lua("text_data",119)
sql2lua("text_data",120)

file.write("\nreturn p")